package com.yeyks.util;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author fuchangshuai
 * @date 2019/3/4 3:41 PM
 */
@Slf4j
public class UnicodeUtils {

    public static String unicodeToString(String unicode) {
        if (!unicode.contains("\\u") && !unicode.contains("\\U")) {
            return unicode;
        }
        unicode = unicode.toLowerCase();
        if ("".equals(unicode)) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        int i = -1;
        int pos = 0;
        while ((i = unicode.indexOf("\\u", pos)) != -1) {
            sb.append(unicode, pos, i);
            if (i + 5 < unicode.length()) {
                pos = i + 6;
                sb.append((char) Integer.parseInt(unicode.substring(i + 2, i + 6), 16));
            }
        }
        return sb.toString();
    }

    public static String stringtoUnicode(String string) {
        if ("".equals(string)) {
            return null;
        }
        StringBuilder unicode = new StringBuilder();
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            unicode.append("\\u").append(Integer.toHexString(c));
        }
        return unicode.toString();
    }

}
